package com.example.black.myapplication.filtercolorrgb;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.black.myapplication.R;

public class BitmapColorRGBFilterView extends View {

    public static final int TRABSLATE_DX = 100;
    private RectF rectF;
    private Paint paint;
    private ColorMatrix colorMatrix;
    private ColorMatrix colorMatrix1;
    private ColorMatrix colorMatrix2;
    private ColorMatrixColorFilter colorMatrixColorFilter;
    private ColorMatrixColorFilter colorMatrixColorFilter1;
    private ColorMatrixColorFilter colorMatrixColorFilter2;
    private ColorMatrixColorFilter colorMatrixColorFilter3;
    private ColorMatrixColorFilter colorMatrixColorFilter4;
    private ColorMatrixColorFilter colorMatrixColorFilter5;
    private Bitmap bitmap;
    private int height;
    private ColorMatrixColorFilter colorMatrixColorFilter6;
    private ColorMatrixColorFilter colorMatrixColorFilter7;
    private ColorMatrixColorFilter colorMatrixColorFilter8;
    private LightingColorFilter lightingColorFilter;

    public BitmapColorRGBFilterView(Context context) {
        super(context);
        initConfig();
    }

    public BitmapColorRGBFilterView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initConfig();
    }

    public BitmapColorRGBFilterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initConfig();
    }

    private void initConfig() {
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon3);
        height = 100 * bitmap.getHeight() / bitmap.getWidth();
        rectF = new RectF(0, 0, 100, height);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //关闭硬件加速才有效果 ？？？
        setLayerType(View.LAYER_TYPE_SOFTWARE,null);//一定要搞清楚，传入paint和不传入的区别

        colorMatrix = new ColorMatrix(new float[]{
                200,0,0,0,0,
                0,1,0,0,10,
                0,0,1,0,0,
                0,0,0,1,0,
        });
        colorMatrix2 = new ColorMatrix(new float[]{
                1.2f,0,0,0,0,
                0,1.2f,0,0,0,
                0,0,1.2f,0,0,
                0,0,0,1.2f,0,
        });
        //反向效果
        colorMatrix1 = new ColorMatrix(new float[]{
                -1,0,0,0,255,
                0,-1,0,0,255,
                0,0,-1,0,255,
                0,0,0,1,255,
        });
        //处理图片为黑白图片（图像学：如何让图片成为灰色即黑白？R+G+B=1）
        /*
         去色原理：只要把RGB三通道的色彩信息设置成一样；即：R＝G
         ＝B，那么图像就变成了灰色，并且，为了保证图像亮度不变，
         同一个通道中的R+G+B=1:如：0.213+0.715+0.072＝1；
         RGB=0.213, 0.715, 0.072；
         三个数字是根据色彩光波频率及色彩心理学计算出来的.
         */
		ColorMatrix matrix3 = new ColorMatrix(new float[]{
				0.213f, 0.715f,0.072f,0,0,
				0.213f, 0.715f,0.072f,0,0,
				0.213f, 0.715f,0.072f,0,0,
				0, 		0,		0,	  1f,0,
		});
        //发色效果（比如红色和绿色交换----把第一行和第二行交换）
		ColorMatrix matrix4 = new ColorMatrix(new float[]{
			0,1f,0,0,0,
			1f,0,0,0,0,
			0,0,1f,0,0,
			0,0,0,1f,0,
		});
        //复古风格
		ColorMatrix matrix5 = new ColorMatrix(new float[]{
				1/2f,1/2f,1/2f,0,0,
				1/3f,1/3f,1/3f,0,0,
				1/4f,1/4f,1/4f,0,0,
				0,0,0,1f,0,
			});
		ColorMatrix matrix6 = new ColorMatrix();
		matrix6.setScale(1,1,1.8f,1);
		ColorMatrix matrix7 = new ColorMatrix();
		//色彩旋转，0是会灰色，增加颜色会增强
		matrix7.setSaturation(0);

        ColorMatrix matrix8 = new ColorMatrix();
        /*
         * axis,代表绕哪一个轴旋转，0,1,2
         * 	(0红色轴，1绿色，2蓝色)
         * degrees：旋转的度数
         */
        matrix8.setRotate(0,80f);
        colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
        colorMatrixColorFilter1 = new ColorMatrixColorFilter(colorMatrix1);
        colorMatrixColorFilter2 = new ColorMatrixColorFilter(colorMatrix2);
        colorMatrixColorFilter3 = new ColorMatrixColorFilter(matrix3);
        colorMatrixColorFilter4 = new ColorMatrixColorFilter(matrix4);
        colorMatrixColorFilter5 = new ColorMatrixColorFilter(matrix5);
        colorMatrixColorFilter6 = new ColorMatrixColorFilter(matrix6);
        colorMatrixColorFilter7 = new ColorMatrixColorFilter(matrix7);
        colorMatrixColorFilter8 = new ColorMatrixColorFilter(matrix8);
        /**
         * mul,multiply相乘 ---缩放
         * add，相加---平移
         * 0x00ff00 绿色缩放
         * 0x000000 不进行平移
         */
        lightingColorFilter = new LightingColorFilter(0x11ff00, 0x000000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.drawBitmap(bitmap,null,rectF,paint);
        canvas.translate(TRABSLATE_DX,0);
        paint.setColorFilter(colorMatrixColorFilter);
        canvas.drawBitmap(bitmap,null,rectF,paint);
        canvas.translate(TRABSLATE_DX,0);
        paint.setColorFilter(colorMatrixColorFilter1);
        canvas.drawBitmap(bitmap,null,rectF,paint);
        canvas.restore();
        canvas.translate(TRABSLATE_DX,height);
        paint.setColorFilter(colorMatrixColorFilter2);
        canvas.drawBitmap(bitmap,null,rectF,paint);
        canvas.translate(TRABSLATE_DX,0);
        paint.setColorFilter(colorMatrixColorFilter3);
        canvas.drawBitmap(bitmap,null,rectF,paint);
        canvas.translate(TRABSLATE_DX,0);
        paint.setColorFilter(colorMatrixColorFilter4);
        canvas.drawBitmap(bitmap,null,rectF,paint);
        canvas.restore();
        canvas.translate(0,height);
        paint.setColorFilter(colorMatrixColorFilter5);
        canvas.drawBitmap(bitmap,null,rectF,paint);

        canvas.translate(0,height);
        paint.setColorFilter(colorMatrixColorFilter6);
        canvas.drawBitmap(bitmap,null,rectF,paint);
        canvas.translate(TRABSLATE_DX,0);
        paint.setColorFilter(colorMatrixColorFilter7);
        canvas.drawBitmap(bitmap,null,rectF,paint);

        canvas.translate(TRABSLATE_DX,0);
        paint.setColorFilter(colorMatrixColorFilter8);
        canvas.drawBitmap(bitmap,null,rectF,paint);

        canvas.translate(TRABSLATE_DX,0);
        paint.setColorFilter(lightingColorFilter);
        canvas.drawBitmap(bitmap,null,rectF,paint);
    }
}
